/*
 * Indiana University Community Grid Computing Lab Software License,Version 1.1
 *
 * Copyright (c) 2002 Community Grid Computing Lab, Indiana University. 
 * All rights reserved.		
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *      "This product includes software developed by the Indiana University 
 *       Community Grid Computing Lab (http://www.communitygrids.iu.edu/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Indiana Univeristy","Indiana Univeristy Pervasive Techonology
 *    Labs" and  "Indiana Univeristy Community Grid Computing Lab" must not be
 *    used to endorse or promote products derived from this software without 
 *    prior written permission. For written permission, please contact 
 *    http://www.communitygrids.iu.edu/.
 *
 * 5. Products derived from this software may not use "Indiana
 *    University" name nor may "Indiana University" appear in their name,
 *    without prior written permission of the Indiana University.
 *
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
 * INDIANA UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package cgl.sensorgrid.narada.publisher;


import java.util.Properties;
import cgl.narada.event.NBEvent;
import cgl.narada.event.NBEventException;
import cgl.narada.event.TemplateProfileAndSynopsisTypes;
import cgl.narada.service.ServiceException;
import cgl.narada.service.client.ClientService;
import cgl.narada.service.client.EventProducer;
import cgl.narada.service.client.SessionService;

public class Publisher {
    private ClientService clientService = null;
    private EventProducer eventProducer = null;
    private int entityId = 7856;
    private Object entityObj = null;
    private String topic = "test/";
    private static boolean initialized = false;

    private static Publisher instance = new Publisher();

    public static Publisher getInstance() {
        return instance;
    }

    private Publisher() {
        String servconfigPath =
                "C:/projects/NaradaBrokering-1.0-RC1/config/ServiceConfiguration.txt";
        int entityId = 7635;
        Properties props = new Properties();
        props.put("hostname", "gf8.ucs.indiana.edu");
        props.put("portnum", "3046");
        String commType = "niotcp";
        initialize(servconfigPath, props, commType, entityId);
    }

    protected void initialize(String serviceConfigFile, Properties props,
                              String commType, int entityId) {
        if (initialized) {
            return;
        }
        this.entityId = entityId;
        entityObj = new Integer(entityId);
        SessionService.setServiceConfigurationLocation(serviceConfigFile);
        try {
            clientService = SessionService.getClientService(entityId);
            clientService.initializeBrokerCommunications(props, commType);
            initializeTransmitter();
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        initialized = true;
    }

    protected void initializeTransmitter() throws ServiceException {
        eventProducer = clientService.createEventProducer();
        eventProducer.setSuppressRedistributionToSource(true);
        eventProducer.generateEventIdentifier(false);
        eventProducer.setDisableTimestamp(true);
        eventProducer.setContentSynopsisInfo(
                TemplateProfileAndSynopsisTypes.STRING, topic);
    }

    public void publishData(byte[] data, String endMsg) {
        NBEvent nbEvent;
        try {
            nbEvent = eventProducer.generateEvent(data);
            nbEvent.getEventProperties().setImmutableProperty("MSG_SEPERATOR",
                    endMsg, entityObj);
            eventProducer.publishEvent(nbEvent);
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NBEventException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Publisher publisher = new Publisher();
        String servconfigPath = "G:/jb2005Projects/research/NaradaBrokering-0_99-rc3/config/ServiceConfiguration.txt";
        int entityId = 7635;
        Properties props = new Properties();
        props.put("hostname", "mastar.ucs.indiana.edu");
        props.put("portnum", "3045");
        String commType = "niotcp";

        publisher.initialize(servconfigPath, props, commType, entityId);
        String str1 = "Test message 1";
        String str2 = "Test message 2";
        String str3 = "Test message 3";
        publisher.publishData("test".getBytes(), "SOF");
        publisher.publishData(str1.getBytes(), "");
        publisher.publishData(str2.getBytes(), "");
        publisher.publishData(str3.getBytes(), "");
        publisher.publishData("test".getBytes(), "EOF");
    }
}
